/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.gui.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.toedo.controllers.DataController;
import org.toedo.gui.translations.Messages;
import org.toedo.gui.views.TaskView;
import org.toedo.utils.ApplicationUtils;

public class OpenProjectAction extends Action {

	public void run() {
		Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		
		FileDialog dialog = new FileDialog(shell, SWT.OPEN);
	    dialog.setFilterExtensions(new String[] {"*.xml", "*.*"}); //$NON-NLS-1$ //$NON-NLS-2$
	    dialog.setFilterNames(new String[] {Messages.OpenAction_XMLFilterLabel, Messages.OpenAction_AllFilesFilterLabel});
	    String fileSelected = dialog.open();
	    
	    if ((fileSelected != null) &&
	    		(!fileSelected.isEmpty())) {
	    	
	    	new UnsavedModifiedCheckerAction(shell).run();
	    	
	    	ApplicationUtils.closeViewByID(TaskView.ID);
	    	DataController.getInstance().open(fileSelected);	    		    	
	    }
	}
}
